package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.CommentsBean;
import com.voute.Model.IGetCommentsModel;
import com.voute.Model.Impl.GetCommentsModelImpl;
import com.voute.Presenter.IGetCommentsPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IGetCommentsView;

import rx.functions.Action0;

/**
 * Created by liucxu on 2017/3/8.
 */

public class GetCommentsPresenterImpl implements IGetCommentsPresenter {

    private IGetCommentsView commentsView;
    private IGetCommentsModel model;

    public GetCommentsPresenterImpl(IGetCommentsView view) {
        commentsView = view;
        model = new GetCommentsModelImpl();
    }

    @Override
    public void getComments(boolean is_newest_comment, String fid, String anchor, String page) {
        if (is_newest_comment) {
            model.getLastedComments(fid, anchor).compose(TransformUtils.<CommentsBean>defaultSchedulers()).doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    commentsView.showLoading();
                }
            }).subscribe(new CommonSubscriber<CommentsBean>() {
                @Override
                public void onSuccess(CommentsBean response) {
                    commentsView.hideLoading();
                    commentsView.requestCommentsSuccess(response);
                }

                @Override
                public void onFailure(BaseBean response) {
                    commentsView.hideLoading();
                    commentsView.showError(response.getMsg());
                }
            });
        } else {
            model.getHotestComments(fid, page).compose(TransformUtils.<CommentsBean>defaultSchedulers()).doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    commentsView.showLoading();
                }
            }).subscribe(new CommonSubscriber<CommentsBean>() {
                @Override
                public void onSuccess(CommentsBean response) {
                    commentsView.hideLoading();
                    commentsView.requestCommentsSuccess(response);
                }

                @Override
                public void onFailure(BaseBean response) {
                    commentsView.hideLoading();
                    commentsView.showError(response.getMsg());
                }
            });
        }
    }
}
